package nz.co.album.converse.method.im;

import java.io.File;
import java.util.HashMap;

import com.tomjudge.TjMSNLib.MSNServerInterface;
import com.tomjudge.TjMSNLib.User;

import nz.co.album.converse.Address;
import nz.co.album.converse.Message;
import nz.co.album.converse.Status;
import nz.co.album.converse.account.im.MSNAccount;
import nz.co.album.converse.address.IMAddress;
import nz.co.album.converse.method.IM;

public class MSN implements IM {

	class MSNClient extends MSNServerInterface {

		private HashMap<String, User> users;
		
		public MSNClient(String handle, String password) {
			super();
			this.signIn(handle, password);
			this.users = new HashMap<String, User>();
		}
		
		public User getUser(String handle) {
			return this.users.get(handle);
		}
		
		@Override
		public void addGroup(String arg0, String arg1) {	}

		@Override
		public void addUser(final User user) {
			this.users.put(user.getHandle(), user);
		}

		@Override
		public void alertFailed(int arg0) {}

		@Override
		public int alertInboundConnection() {
			return DECLINE;
		}

		@Override
		public int alertInboundTransfer(String arg0, String arg1, String arg2) {
			return DECLINE;
		}

		@Override
		public int alertOutboundConnection() {
			return DECLINE;
		}

		@Override
		public void alertTransferComplete(int arg0) {}

		@Override
		public File getInboundFile(String arg0) {
			return null;
		}

		@Override
		public void imRecived(int arg0, User arg1, String arg2, String arg3) {}

		@Override
		public void increaseAmountTransfered(int arg0, long arg1) {}
		
		@Override
		public void removeGroup(String arg0) {}

		@Override
		public void removeUser(User arg0) {}

		@Override
		public void renameGroup(String arg0, String arg1) {}

		@Override
		public void setClientDisplayName(String arg0) {}

		@Override
		public void setClientStatus(int arg0) {}

		@Override
		public void setDisplayMenus(int arg0) {}

		@Override
		public void setDisplaySigendOut() {}

		@Override
		public void setFileSize(int arg0, long arg1) {}

		@Override
		public void showError(String arg0, String arg1) {}

		@Override
		public int userAddedYou(User arg0) {
			return ACCEPT;
		}

		@Override
		public String userAddedYouGetGroup(User arg0) {
			return null;
		}

		@Override
		public void userJoinedIm(int arg0, User arg1) {}

		@Override
		public void userTyping(int arg0, User arg1) {}
		
	}
	
	private MSNClient client;
	
	public MSN(MSNAccount account) {
		client = new MSNClient(account.getHandle(), account.getPassword());
	}
	
	public Status queryStatus(IMAddress address) {
		switch(client.getUser(address.getHandle()).getState()) {
			case MSNServerInterface.FLN:
			case MSNServerInterface.HDN:
				return Status.OFFLINE;
			
			case MSNServerInterface.NLN:
				return Status.ONLINE;
			
			default:
				return Status.AWAY;
		}
	}

	public void sendMessage(Address address, Message message) {
		client.sendIm(client.requestSession(client.getUser(((IMAddress) address).getHandle())), message.toString());
	}

}
